package sz.szsmk.citizencard.ui.fragment;

import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.location.LocationManager;
import android.os.Bundle;
import android.provider.Settings;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import java.util.ArrayList;
import java.util.List;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.BusNearbyLineViewBinding;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonFragmentComponent;
import sz.szsmk.citizencard.model.network.bean.response.QueryBusLineNearbyRes;
import sz.szsmk.citizencard.mvp.contract.INearbyLineContact;
import sz.szsmk.citizencard.mvp.presenter.NearbyLinePresenter;
import sz.szsmk.citizencard.ui.adapter.BusNearbyLineAdapter;
import sz.szsmk.citizencard.ui.base.IBaseFragment;
import sz.szsmk.citizencard.util.PermissionPageUtils;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/11/15 10:22
 * description:
 */
public class BusNearbyLineFragment extends IBaseFragment <NearbyLinePresenter,BusNearbyLineViewBinding>
        implements INearbyLineContact.INearbyLineView {

    RecyclerView nearbyLineRv;
    SmartRefreshLayout swipeRefreshLayout;
    private LocationManager lm;//【位置管理】
    private TextView emptyTv;
    private LinearLayout empayView;
    private String mLatitude;
    private String mLongitude;
    private List<QueryBusLineNearbyRes.BusLineArrayListBean> busLineArrayList = new ArrayList<>();
    private BusNearbyLineAdapter busNearbyLineAdapter;
    private int firstIn;

    public static BusNearbyLineFragment getInstance() {
        BusNearbyLineFragment sf = new BusNearbyLineFragment();
        return sf;
    }

    @Override
    protected int attachLayoutRes() {
        return R.layout.bus_nearby_line_view;
    }

    @Override
    protected void initInjector() {
        DaggerCommonFragmentComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {

        nearbyLineRv =  dataBinding.getRoot().findViewById(R.id.nearby_line_rv);
        swipeRefreshLayout=  dataBinding.swipeRefreshLayout ;
        emptyTv = (TextView) dataBinding.getRoot().findViewById(R.id.empty_tv);
        empayView = (LinearLayout) dataBinding.getRoot().findViewById(R.id.empty_view);
        emptyTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PermissionPageUtils permissionPageUtils = new PermissionPageUtils(getActivity());
                permissionPageUtils.jumpPermissionPage();
            }
        });
        busNearbyLineAdapter = new BusNearbyLineAdapter(getActivity());
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getActivity());

        dataBinding.nearbyLineRv.setLayoutManager(linearLayoutManager);
        dataBinding.nearbyLineRv.setAdapter(busNearbyLineAdapter);

        dataBinding.swipeRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                mPresenter.queryBusLineNearby(mLatitude, mLongitude);
            }
        });
        dataBinding.swipeRefreshLayout.autoRefresh();
    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        firstIn = 0;//0表示第一次进入，1表示请求过权限
    }

    @Override
    public void onResume() {
        super.onResume();
        lm = (LocationManager) getActivity().getSystemService(getActivity().LOCATION_SERVICE);
        boolean ok = lm.isProviderEnabled(LocationManager.GPS_PROVIDER);
        if (ok) {//开了定位服务
            if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION)
                    != PackageManager.PERMISSION_GRANTED) {
                dataBinding.nearbyLineRv.setVisibility(View.GONE);
                empayView.setVisibility(View.VISIBLE);

            } else {
                dataBinding.nearbyLineRv.setVisibility(View.VISIBLE);
                empayView.setVisibility(View.GONE);

            }
        } else {
            if (firstIn == 0) {
                Toast.makeText(getActivity(), "系统检测到未开启GPS定位服务", Toast.LENGTH_SHORT).show();
                Intent intent = new Intent();
                intent.setAction(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
                startActivityForResult(intent, 1315);
                firstIn = 1;
            }

        }

    }

    @Override
    public void queryBusLineNearbyFail(QueryBusLineNearbyRes response) {

        //完成加载并标记没有更多数据
        dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
    }

    @Override
    public void queryBusLineNearbySucc(QueryBusLineNearbyRes response) {

        busLineArrayList = response.getBusLineArrayList();
        busNearbyLineAdapter.setViewData(busLineArrayList);
        busNearbyLineAdapter.notifyDataSetChanged();
        //完成加载并标记没有更多数据
        dataBinding.swipeRefreshLayout.finishLoadMoreWithNoMoreData();
    }

}
